<?php
$baseURL = './CentralController.php?surveycode=Surveys';
require_once ROOT_PATH . '/lib/models/surveys/Answers.php';//Chinh Nguyen add
require_once ROOT_PATH . '/lib/models/hrfunct/EmpInfo.php';


function getSurveyType($type){
	if($type ==0)
		return "Survey";
	elseif($type==1)
		return "Poll";
	else
		return "Quiz"; 	
}  
 ?>
<script type="text/javascript">
 function returnAdd() {
		location.href = '<?php echo $baseURL;?>&action=Define';
}
function returnDelete() {
		var check = false;
		with (document.frmSurveys) {
			for (var i=0; i < elements.length; i++) {
				if ((elements[i].type == 'checkbox') && (elements[i].checked == true)){
					check = true;
					break;
				}
			}

			if (check) {
				document.frmSurveys.action= "<?php echo $_SERVER['PHP_SELF']; ?>?surveycode=Surveys&action=DeleteSurveys";
				document.frmSurveys.submit();
			} else {
				alert("<?php echo $lang_Error_SelectAtLeastOneRecordToDelete; ?>");
			}
		}
}

function returnCopy() {
		
		var check = false;
		with (document.frmSurveys) {
			for (var i=0; i < elements.length; i++) {
				if ((elements[i].type == 'checkbox') && (elements[i].checked == true)){
					check = true;
					break;
				}
			}

			if (check) {
				document.frmSurveys.action= "<?php echo $_SERVER['PHP_SELF']; ?>?surveycode=Surveys&action=CopySurveys";
				document.frmSurveys.submit();
			} else {
				alert("<?php echo $lang_Error_SelectAtLeastOneRecordToCopy; ?>");
			}
		}
}
 </script>
<h2><?php echo $lang_Surveys_List; ?><hr/></h2>
<?php if (isset($_GET['message'])) {		
		$expString  = $_GET['message'];
		$_GET['message']="";	
?>
		<font class="" size="-1" face="Verdana, Arial, Helvetica, sans-serif">
<?php echo $expString; 

?>
		</font>
<?php }	?>
<form id="frmSurveys" name="frmSurveys" method="post" action="">
<div style="padding: 6px;">
				<img 
					title="Add" 
					alt="Add"
					src="../../themes/beyondT/pictures/btn_add.gif" 
					style="border: none;"
					onclick="returnAdd();"
					onmouseout="this.src='../../themes/beyondT/pictures/btn_add.gif';" 
					onmouseover="this.src='../../themes/beyondT/pictures/btn_add_02.gif';" />	
					 
					<img 
						title="Delete" 
						alt="Delete" 
						src="../../themes/beyondT/pictures/btn_delete.gif" 
						style="border: none;"
						onclick="returnDelete();"
						onmouseout="this.src='../../themes/beyondT/pictures/btn_delete.gif';"
						onmouseover="this.src='../../themes/beyondT/pictures/btn_delete_02.gif';" />
					<img 
						title="Copy" 
						alt="Copy" 
						src="../../themes/beyondT/pictures/btn_copy.gif" 
						style="border: none;"
						onclick="returnCopy();"
						onmouseout="this.src='../../themes/beyondT/pictures/btn_copy.gif';"
						onmouseover="this.src='../../themes/beyondT/pictures/btn_copy_02.gif';" />
					
</div>
<div style="text-align: left; padding-top: 4px; width: 98%">
<p class="page-navigation link04 lite">
				<?php
					if ($records['totalSurveys']>0) {
						$totalPage = $records['totalSurveys']/10;
						if($totalPage >1){
						for ( $page = 0; $page <= $totalPage; $page ++ )
						{	
							if($page == $records['page'])
								echo "|<a style='text-decoration:none;' href='CentralController.php?surveycode=Surveys&action=List&page={$page}'><span style='font-weight:bold;'>"." ".$page." "."</span></a>|";
							else 
								echo "|<a  href='CentralController.php?surveycode=Surveys&action=List&page={$page}'><span stlye='border:1px solid #EEEEEE;'>"." ".$page." "."</span></a>|";
						}
						}
					}		
				?>
</p>
</div>		
<table border="0" cellpadding="0" cellspacing="0">
  <thead>
  	<tr>
		<th class="tableTopLeft"></th>
    	<th class="tableTopMiddle" width="10"></th>
    	<th class="tableTopMiddle" width="150"></th>
    	<th class="tableTopMiddle" ></th>
    	<th class="tableTopMiddle" width="150"></th>
    	<th class="tableTopMiddle" ></th>
    	<th class="tableTopMiddle" ></th>
    	<th class="tableTopMiddle" ></th>              	
		<th class="tableTopRight"></th>
	</tr>
	<tr>
		<th class="tableMiddleLeft"></th>
		<th class="tableMiddleMiddle"></th>
    	<th  class="tableMiddleMiddle"><?php echo $lang_Survey_Name; ?></th>
    	<th class="tableMiddleMiddle">Survey type</th>
    	<th  class="tableMiddleMiddle"><?php echo $lang_Survey_Duration; ?></th>    	
    	<th class="tableMiddleMiddle"><?php echo $lang_Survey_Owner; ?></th>
    	<th class="tableMiddleMiddle"><?php echo "Results"; ?></th>
    	<th class="tableMiddleMiddle"><?php echo "Status"; ?></th>
		<th class="tableMiddleRight"></th>
	</tr>
  </thead>
  <tbody>
<?php
	$j = 0;
	if (count($records['surveys'])>0) {		
		 $today = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
		 
		foreach ($records['surveys'] as $record) {
			//$surveyOwner = 
			if(!($j%2)) {
				$cssClass = 'odd';
			 } else {
			 	$cssClass = 'even';
			 }
			 $j++;
			 
			 $dayleft = (strtotime($record->getSurveyToDate())- $today)/ 86400;
			 
		
?>
  <tr>
  	<td class="tableMiddleLeft"></td>
  	<td class="<?php echo $cssClass?>" align="center"> <input type='checkbox' class='checkbox' name='chkID[]' value='<?php echo $record->getId();?>'></td>
  	
    <td class="<?php echo $cssClass; ?>"><a href="CentralController.php?surveycode=Surveys&action=ShowEditSurvey&id=<?php echo $record->getId(); ?>" target="rightMenu"><?php echo $record->getSurveyName(); ?></a></td>
    <td class="<?php echo $cssClass; ?>"><?php echo getSurveyType($record->getSurveyType()); ?></td>
    <td class="<?php echo $cssClass; ?>"><?php echo date('m/d/Y',strtotime($record->getSurveyFromDate()))."->".date('m/d/Y',strtotime($record->getSurveyToDate())); ?></td>
    <td class="<?php echo $cssClass; ?>"><?php  echo EmpInfo::getFullName($record->getSurveyOwner());  ?></td>
    
    <?php
    	$totalAnswer =  Answers::getTotalAnswersBySurvey($record->getId());
    	if($totalAnswer>0){ ?>
    <td class="<?php echo $cssClass; ?>"><a href="CentralController.php?surveycode=Surveys&action=ShowResultsDetail&id=<?php echo $record->getId(); ?>" target="rightMenu"><?php  echo $totalAnswer; ?></a></td>
    <?php } else{ ?>
    	<td class="<?php echo $cssClass; ?>"><?php  echo $totalAnswer;  ?></td>
    <?php } ?>
    <?php if($record->getSurveyActive()==0 ){ ?>
    	<td class="<?php echo $cssClass; ?>">This survey is inactive</td>
    <?php }elseif($dayleft <0){ ?>
    	<td class="<?php echo $cssClass; ?>">This survey is expired</td>
    <?php }elseif(!$record->getSurveyUsers()){ ?>
    	<td class="<?php echo $cssClass; ?>">Please invite users</td>
     <?php }elseif($record->getSurveyActiveEmail() ==0){ ?>
    	<td class="<?php echo $cssClass; ?>">Survey invitation sent</td>
    <?php }else{ ?>
			<td class="<?php echo $cssClass; ?>"><a href="CentralController.php?surveycode=Surveys&action=SendInvitation&id=<?php echo $record->getId(); ?>" target="rightMenu"><?php echo "Pending send survey invitation"; ?></a></td>    		
    <?php } ?>
	<td class="tableMiddleRight"></td>
  </tr>

<?php
		}
	}else{ ?>
		<tr>
  	<td class="tableMiddleLeft"></td>
  	<td colspan="7"> No survey created. Please add new survey</td>    
	<td class="tableMiddleRight"></td>
  </tr>
		
<?php	}
?>
  </tbody>
  <tfoot>
  	<tr>
		<td class="tableBottomLeft"></td>
		<td class="tableBottomMiddle"></td>
		<td class="tableBottomMiddle"></td>
		<td class="tableBottomMiddle"></td>
		<td class="tableBottomMiddle"></td>
		<td class="tableBottomMiddle"></td>
		<td class="tableBottomMiddle"></td>
		<td class="tableBottomMiddle"></td>							
		<td class="tableBottomRight"></td>
	</tr>
  </tfoot>
</table>
<div style="text-align: left; padding-top: 4px; width: 98%">
<p class="page-navigation link04 lite">
				<?php
					if ($records['totalSurveys']>0) {
						$totalPage = $records['totalSurveys']/10;
						if($totalPage >1){
						for ( $page = 0; $page <= $totalPage; $page ++ )
						{	
							if($page == $records['page'])
								echo "|<a style='text-decoration:none;' href='CentralController.php?surveycode=Surveys&action=List&page={$page}'><span style='font-weight:bold;'>"." ".$page." "."</span></a>|";
							else 
								echo "|<a  href='CentralController.php?surveycode=Surveys&action=List&page={$page}'><span stlye='border:1px solid #EEEEEE;'>"." ".$page." "."</span></a>|";
						}
						}
					}		
				?>
</p>
</div>		

